﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Steam4NET;

namespace GLaDOSAtHomeCommunity
{
    public class GameDetection
    {
        private static ISteamClient009 steamClient;
        private static ISteamApps004 steamApps;
        private static int hPipe, hUser;

        public static bool SteamConnect()
        {
            try
            {
                return InitSteam();
            }
            catch (BadImageFormatException e) { return false; }
        }

        public static bool SteamOwnsGame(UInt32 id)
        {
            return steamApps.IsSubscribedApp(id);
        }

        public static void SteamDisconnect()
        {
            steamApps = null;
        }

        private static bool InitSteam()
        {
            if (!Steamworks.Load())
                return false;

            steamClient = Steamworks.CreateInterface<ISteamClient009>();
            if (steamClient == null)
                return false;

            hPipe = steamClient.CreateSteamPipe();
            hUser = steamClient.ConnectToGlobalUser(hPipe);

            steamApps = steamClient.GetISteamApps<ISteamApps004>(hUser, hPipe);
            if (steamApps == null)
                return false;

            return true;
        }
    }
}
